package com.yijiu.trb;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.RequestAccountInfoResponse;

/**
 * 划出失败页面
 * 
 * @author TangChao
 * 
 */
public class OutInDoneFailActivity extends BTitleActivity
{
	private TextView moneyTextView;

	private String flag;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_line_out_done);
		findViewsInit();
		requestAccountInfo(CacheData.APP_KEY);
	}

	private void findViewsInit()
	{
		Button doGoOn = (Button) findViewById(R.id.btn_out_in_done_fail_go_on);
		TextView statusTextView = (TextView) findViewById(R.id.tv_out_in_done_fail_text);
		moneyTextView = (TextView) findViewById(R.id.tv_out_in_done_fail_money);
		flag = getIntent().getStringExtra("flag");
		if ("cutInDone".equals(flag))
		{
			titleView.setText("划入成功");
			statusTextView.setText("划入成功");
			statusTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.out_in_done, 0, 0, 0);
			doGoOn.setText("继续划入");
		}
		else if ("cutInFail".equals(flag))
		{
			titleView.setText("划入失败");
			statusTextView.setText("划入失败");
			statusTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.out_in_fail, 0, 0, 0);
			doGoOn.setText("继续划入");
		}
		else if ("lineOutDone".equals(flag))
		{
			titleView.setText("划出成功");
			statusTextView.setText("划出成功");
			statusTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.out_in_done, 0, 0, 0);
			doGoOn.setText("继续划出");
		}
		else if ("lineOutFail".equals(flag))
		{
			titleView.setText("划出失败");
			statusTextView.setText("划出失败");
			statusTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.out_in_fail, 0, 0, 0);
			doGoOn.setText("继续划出");
		}
		String errorMsg = getIntent().getStringExtra("errorMsg");
		if (!StringUtils.isNull(errorMsg))
		{
			new AlertDialog.Builder(OutInDoneFailActivity.this).setTitle("提示").setMessage(errorMsg)
					.setPositiveButton("确定", new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					}).create().show();
		}
	}

	public void outInDoneFailClick(View v)
	{
		int id = v.getId();
		switch (id)
		{
			case R.id.btn_out_in_done_fail_go_on:
				if (flag.contains("cutIn"))
					initIntent(this, CutIn.class);
				else
					initIntent(this, LineOut.class);
				break;
			case R.id.btn_out_in_done_fail_back_home:
				rightButtonClick();
				break;
			default:
				break;
		}
	}

	@Override
	protected void leftButtonClick()
	{
		if (flag.contains("cutIn"))
			initIntent(this, CutIn.class);
		else
			initIntent(this, LineOut.class);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if (flag.contains("cutIn"))
				initIntent(this, CutIn.class);
			else
				initIntent(this, LineOut.class);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 账户信息请求
	 * 
	 * @param appKey
	 */
	private void requestAccountInfo(String appKey)
	{
		String inputStr = "service=queryAcountInfo";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestAccountInfoResponse requestAccountInfoResponse = new RequestAccountInfoResponse();
		Request request = new Request(action, requestAccountInfoResponse, ActionType.REQUEST_ACCOUNT_INFO);
		netManager.excute(request, this, this);
		showProgress("正在获取账户信息");
	}

	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{

			case ActionType.REQUEST_ACCOUNT_INFO:// 请求账户信息
				RequestAccountInfoResponse requestAccountInfoResponse = (RequestAccountInfoResponse) r.getResponse();
				CacheData.AVAILABLE_BALANCE = requestAccountInfoResponse.field.availableBalance.toString();
				CacheData.CERTIFY_STATUS = requestAccountInfoResponse.field.certifyStatus;
				moneyTextView.setText("账户余额：￥" + CacheData.AVAILABLE_BALANCE);
				break;
		}
	}

	/**
	 * 操作失败返回的函数
	 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.REQUEST_ACCOUNT_INFO:
				app.showMsg(errorMsg);
				dismissProgress();
				break;
		}
	}

	/**
	 * 重复提交返回的函数
	 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
}
